<template>
	<view class="layout">
		<view class="center-list-header">
			<uni-search-bar radius="100" placeholder="请输入客户名称或联系人" cancelButton="none" @input="onInput" @confirm="search"
				:focus="true" v-model="searchValue" @clear="clear">
			</uni-search-bar>
		</view>

		<view class="center-list-main" v-if='tableData && tableData.length>0'>
			<view v-for="(item,index) in tableData" :key="item.quotation_id">
				<quotationListItem :key="item.quotation_id" :data="item" @itemClick="itemClick" @oprateClick="oprateClick">
				</quotationListItem>
			</view>
		</view>

		<view class="center-list-main" v-else>
			<div class='nodata'>
				<image src='../../../static/noBill.png' class="noImage" />
				<text class="noText">暂无报价记录</text>
			</div>
		</view>

		<view v-if="selectData.length" class="button-group">
			<view style="color: #116BFF;" @click="deleteData">
				清空
			</view>
			<view class="text">
				已选数量：{{selectData.length}}
			</view>
			<p @click='exportBill'>导出报价单</p>
		</view>
		<exportTemplate v-if="selectData.length" :propData="exportData" />
	</view>
</template>

<script>
	import {
		request_quotation,
		delete_quotation
	} from '@/common/api.js';
	import quotationListItem from '@/components/quotationListItem/quotationListItem.vue';
	import exportTemplate from './exportTemplate.vue';
	import html2canvas from 'html2canvas';
	export default {
		name: 'quotationList',
		components: {
			quotationListItem,
			exportTemplate
		},
		data() {
			return {
				searchValue: "",
				contentH: "0rpx",
				allData: [],
				tableData: [],
				selectData: [],
				exportData: {}
			};
		},
		onLoad() {
			let _this = this
			uni.getSystemInfo({
				success: function(res) {
					console.log('导航条高度:', res);
					_this.contentH = res.windowHeight * 2 - 120 - 148 + 'rpx'
				}
			});
			this.getMyList()
		},
		// 下拉刷新
		async onPullDownRefresh() {
			console.log('下拉刷新');
		},
		methods: {
			getMyList(val = '') {
				request_quotation().then(res => {
					console.log(res);
					if (res.code != '200') return uni.showToast({
						icon: 'error',
						title: res.msg || '服务异常。'
					})
					this.allData = res.data.quotation_list || []
					this.tableData = this.allData
				})
			},
			onInput(event) {
				this.tableData = this.allData.filter(e => {
					if (e.custom_name.includes(event) || e.custom_contact.includes(event)) return e
				})
			},
			search() {
				console.log(this.searchValue)
				if (!this.searchValue) return this.getMyList()
				this.tableData = this.allData.filter(e => {
					if (e.custom_name.includes(this.searchValue) || e.custom_contact.includes(this.searchValue)) return e
				})
				this.selectData = []
			},
			clear() {
				this.searchValue = '';
				this.getMyList()
			},
			itemClick(val) {
				console.log('itemClick', val)
				// return
				let params = encodeURIComponent(JSON.stringify(val.data))
				uni.navigateTo({
					url: `/pages/userModule/quotedDetail/quotedDetail?params=${params}`
				})
			},
			oprateClick(val) {
				if (val.checked) return this.selectData.push(val.data)
				this.selectData = this.selectData.filter(e => e.quotation_id !== val.data.quotation_id)
			},
			deleteData() {
				uni.showModal({
					title: '确认删除？',
					content: '删除后无法恢复',
					success: (res) => {
						if (res.confirm) {
							delete_quotation({
								quotation_ids: this.selectData.map(e => e.quotation_id)
							}).then(res => {
								if (res.code != '200') return uni.showToast({
									icon: 'none',
									title: res.msg || '服务异常。'
								})
								this.selectData = []
								this.search()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			exportBill() {
				let custom_name = this.selectData[0].custom_name
				if (this.selectData.some(e => e.custom_name != custom_name)) {
					uni.showToast({
						title: '分属不同客户',
						icon: 'none',
						duration: 2000
					});
					setTimeout(function() {
						uni.hideToast();
					}, 2000);
					return
				}

				let formulaList = uni.getStorageSync('formula');
				formulaList = JSON.parse(formulaList);
				let list = this.selectData.map(e => {
					e.boxName = formulaList.find(ee => ee.formula == e.formula).name
					return e
				})
				this.exportData = {
					list,
					...this.selectData[0]
				}

				// #ifndef H5
				uni.showToast({
					title: '暂未开放，敬请期待！',
					icon: 'none',
					duration: 2000
				});
				// #endif
				// #ifdef H5
				setTimeout(() => this.doPrint(), 300)
				// #endif
			},
			doPrint() {
				html2canvas(document.getElementById('exportTemplate')).then((canvas) => {
					const img = canvas.toDataURL('image/png');
					const a = document.createElement('a')
					a.download = '报价记录'
					a.href = img
					document.body.appendChild(a)
					a.click()
					document.body.removeChild(a)
					// 在这里可以使用生成的图片链接（img）进行下载或其他操作  
				});
				// console.log(document.getElementById('exportTemplate').toDataURL('image/png'))
			}
		}
	}
</script>
<style scoped lang="scss">
	.nodata {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;

		.noImage {
			width: 240rpx;
			height: 240rpx;
		}

		.noText {
			margin-top: 10rpx;
			font-size: 28rpx;
			height: 44rpx;
			line-height: 44rpx;
			font-family: Source Han Sans CN, Source Han Sans CN;
			font-weight: 400;
			color: #999999;
		}
	}

	.layout {
		background: #F8F8F8;
		padding: 120rpx 16rpx 160rpx;
		box-sizing: border-box;
		height: 100%;

		.center-list-header {
			// height: 120rpx;
			// position: fixed;
			// padding: 40rpx 0 0 0;
			box-sizing: border-box;
			width: 100%;
			left: 0;
			top: 0;
			background: #fff;
			z-index: 10;
			border-bottom: 1px solid #E7E7E7;

			.uni-searchbar {
				padding: 16rpx 32rpx;
			}
		}

		.center-list-main {
			width: 100%;
			height: 100%;
			overflow: auto;

			.lineBox {
				width: 100%;
				display: flex;
				padding: 20rpx;
				box-sizing: border-box;
				background-color: #fff;
				margin-bottom: 20rpx;
				border-radius: 8rpx;
				min-height: 100rpx;
				height: 68px;

				.item-right {
					display: flex;
					align-items: center;

					>image {
						width: 40rpx;
						height: 40rpx;
						background: rgba(255, 255, 255, 0.01);
					}
				}

				.item-left {
					flex: 1;
					position: relative;
					padding: 0 0 0 96rpx;
					box-sizing: border-box;

					>image {
						position: absolute;
						width: 96rpx;
						height: 96rpx;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}

					>view {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						height: 100%;
						margin-left: 20rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #333333;
					}

					.bottom-txt {
						font-size: 28rpx;
						font-weight: 400;
						color: #999999;
						line-height: 32rpx;
					}
				}

				&:last-child {
					margin-bottom: 0;
				}

			}
		}

		.button-group {
			width: 100%;
			// position: fixed;
			background-color: #fff;
			left: 0;
			bottom: 0;
			padding: 32rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			height: 148rpx;
			z-index: 9;

			.text {
				margin: 0 20rpx;
			}

			>p {
				flex: 1;
				text-align: center;
				font-size: 28rpx;
				color: #FFFFFF;
				width: 340rpx;
				height: 92rpx;
				line-height: 92rpx;
				background: linear-gradient(313deg, #0060FF 0%, #4289FF 100%);
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				opacity: 1;
			}

		}
	}
</style>
<style>
	.layout {
		position: absolute;
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
		padding: 0 !important;

		.center-list-header {
			padding: 0;
			/* position: relative !important; */
			height: unset;
		}

		.center-list-main {
			flex: 1;
		}

		.button-group {
			position: relative;
		}
	}
</style>